package com.mc.model;

import java.util.List;

/***
 * 翻页类，用于计算翻页索引
 */
public class Paging<T> {
	/**页码索引*/
	private int pageIndex = 1;
	/**每页最多数量*/
	private int size = 10;
	/**分页的数据*/
	private List<T> dates;
	/**数据总量*/
	private long dateCount;
	
	/**获取分页的数据*/
	public List<T> getDates() {
		return dates;
	}
	
	public void setDates(List<T> list) {
		dates = list;
	}

	
	/**获取起始索引*/
	public int getStartIndex() {
		return (pageIndex - 1) * size;
	}
	
	/**获取上一页页码索引*/
	public int getPreIndex() {
		return pageIndex - 1;
	}
	
	/**获取下一页页码索引*/
	public int getNextIndex() {
		return pageIndex + 1;
	}
	
	public int getPageIndex() {
		if (pageIndex < 1 )
			pageIndex = 1;
		return pageIndex;
	}
	
	public void setPageIndex(int pageIndex) {
		if (pageIndex < 1)
			pageIndex = 1;
		this.pageIndex = pageIndex;
	}
	
	public void setPageIndex(Integer pageIndex) {
		if (pageIndex == null)
			pageIndex = 1;
		this.pageIndex = pageIndex;
	}
	
	/**为方面接受页面参数，特增设接受p参数*/
	public void setP(int pageIndex) {
		this.pageIndex = pageIndex;
	}
	
	/**为方面接受页面参数，特增设接受page参数*/
	public void setPage(int pageIndex) {
		this.pageIndex = pageIndex;
	}

	public int getSize() {
		return size;
	}

	public void setSize(int size) {
		this.size = size;
	}

	public long getDateCount() {
		return dateCount;
	}

	public void setDateCount(long dateCount) {
		this.dateCount = dateCount;
	}

	/**获取总的页数*/
	public long getPageCount() {
		if (dateCount % size > 0)
			return dateCount / size + 1;
		return dateCount / size;
	}
	
	
	public String getSimplePaging() {
		String pre = pageIndex - 1 > 0 ? "<a href='?p=1'>首页</a> <a href='?p="+getPreIndex()+"'>上一页</a>" : "<font color='#cccccc'>首页 上一页 </font>";
		String next = pageIndex + 1 <= getPageCount() ? "<a href='?p="+getNextIndex()+"'>下一页</a>" +
				" <a href='?p="+getPageCount()+"'>末页</a>" : "<font color='#cccccc'>下一页 末页</font>";
		String str = pre + " " + next +
				" 每页:"+getSize()+"/<span style='color:red'>"+getDateCount()+"</span>条  " +
				"页数:<span style='color:blue'>"+getPageIndex()+"</span>/"+getPageCount()+"页";
		
		return str;
		
	}
	
}
